import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.StringTokenizer;

/**
 * 
 * @author MT
 *
 */
public class GatherMergeSort {

	private static ArrayList<Person> data;
	
/*	public static void gatherMergeAndSort(File f, String type) {
		// perform operation
		gatherData(f, type);

		// sort and display results		
	}
*/	
	private static void displayData() {
		for(Person p : data) {
			System.out.println(p.toString());
		}
	}

	public static void gatherData(File f, String type) {
		// as you read each line, create objects and insert into array
		BufferedReader br = null;
		
		if(data == null) {
			data = new ArrayList<Person>();
		}
		
		try {
			br = new BufferedReader(new FileReader(f));
			String line;
			Person p;
			
			while((line = br.readLine()) != null) {
				if(type == "pipe") {
					p = scanPipeLine(line);
				} else if(type == "comma") {
					p = scanCommaLine(line);
				} else {
					p = scanSpaceLine(line);
				}
				
				data.add(p);
			}
		} catch (IOException e){
			e.printStackTrace();
		}
		
	}
	
	public static Person scanPipeLine(String line) {
		StringTokenizer st = new StringTokenizer(line, " | ");
		String ln = (String) st.nextElement();
		String fn = (String) st.nextElement();
		st.nextElement();
		String gender = parseGender((String) st.nextElement());
		String clr = (String) st.nextElement();
		String dob = (String) st.nextElement();

		return new Person(ln, fn, gender, dob, clr);
	}
	
	public static Person scanCommaLine(String line) {
		StringTokenizer st = new StringTokenizer(line, ", ");
		String ln = (String) st.nextElement();
		String fn = (String) st.nextElement();
		String gender = (String) st.nextElement();
		String clr = (String) st.nextElement();
		String dob = ((String) st.nextElement()).replace('/', '-');

		return new Person(ln, fn, gender, dob, clr);
	}

	public static Person scanSpaceLine(String line) {
		StringTokenizer st = new StringTokenizer(line);
		String ln = (String) st.nextElement();
		String fn = (String) st.nextElement();
		st.nextElement();
		String gender = parseGender((String) st.nextElement());
		String dob = (String) st.nextElement();
		String clr = (String) st.nextElement();

		return new Person(ln, fn, gender, dob, clr);
	}
	
	private static String parseGender(String character) {
		if(character.equals("F")) {
			return "Female";
		} else {
			return "Male";
		}
	}
	
	public static void main(String[] args) {
		gatherData(new File("input_files/comma.txt"), "comma");
		gatherData(new File("input_files/pipe.txt"), "pipe");
		gatherData(new File("input_files/space.txt"), "space");
		
		System.out.println("Output 1:");
		BeanComparator bc3 = new BeanComparator(Person.class, "getGender");
		BeanComparator bc4 = new BeanComparator(Person.class, "getLastName");
		GroupComparator gc = new GroupComparator(bc3, bc4);
		Collections.sort(data, gc);
		
		displayData(); // sort by gender and last name

		System.out.println("\nOutput 2:");
		BeanComparator bc2 = new BeanComparator(Person.class, "getDob");
		Collections.sort(data, bc2);
		
		displayData(); // sort by dob		

		System.out.println("\nOutput 3:");
		BeanComparator bc = new BeanComparator(Person.class, "getLastName", false);
		Collections.sort(data, bc);
		
		displayData(); // sort by last name descending
	}
}
